tensorflow的keras实现搭配dataset 之二

tensorflow的keras实现搭配dataset,几种形式都工作!

讨论 tensorflow的keras 函数式,而不去讨论原生keras的,因为原生的keras的与dataset的搭配不好!

定义函数模型的方式有两种,其中一种能让原生的keras与dataset很好工作,另一种不能;本文讨论

tensorflow的keras与dataset花式搭配,感觉好自由哦!

 

 

from tensorflow import keras as ks
import tensorflow as tf

# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20)).astype(np.float32)
y_train = ks.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10).astype(np.float32)
x_test = np.random.random((100, 20)).astype(np.float32)
y_test = ks.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10).astype(np.float32)


batch_size = 100
steps_per_epoch = int(np.ceil(x_train.shape[0]/batch_size))

train_ds = tf.data.Dataset.from_tensor_slices((x_train,y_train))
train_ds = train_ds.batch(batch_size).repeat()   # batch 能给数据集增加批维度
train_it = train_ds.make_one_shot_iterator()
x_train_it, y_train_it = train_it.get_next()


test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_ds = test_ds.batch(batch_size).repeat()
test_it = train_ds.make_one_shot_iterator()
x_test_it, y_test_it = test_it.get_next()


def gen_model1():
    model_input = ks.layers.Input(shape=(20,))
    x = ks.layers.Dense(64, activation='relu')(model_input)
    x = ks.layers.Dropout(0.5)(x)
    x = ks.layers.Dense(64, activation='relu')(x)
    x = ks.layers.Dropout(0.5)(x)
    model_output = ks.layers.Dense(10, activation='softmax')(x)
    train_model = tf.keras.models.Model(inputs=model_input, outputs=model_output)
    sgd = ks.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    train_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
    train_model.summary()
    return train_model

# passing the data to the model with the below to style, both work
model = gen_model1()
model.fit(x_train_it, y_train_it, epochs=20, steps_per_epoch=steps_per_epoch)
score = model.evaluate(test_ds, steps=128)
print(score)
print("(+("*20,'\n'*4)
model.fit(train_ds, epochs=20, steps_per_epoch=steps_per_epoch)

score = model.evaluate(test_ds, steps=128)
print(score)

print("\n"*6)

def gen_model2(inputs, targets):
    model_input = ks.layers.Input(tensor=inputs)
    x = ks.layers.Dense(64, activation='relu')(model_input)
    x = ks.layers.Dropout(0.5)(x)
    x = ks.layers.Dense(64, activation='relu')(x)
    x = ks.layers.Dropout(0.5)(x)
    model_output = ks.layers.Dense(10, activation='softmax')(x)
    train_model = tf.keras.models.Model(inputs=model_input, outputs=model_output)
    sgd = ks.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    train_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'],target_tensors=[targets])
    train_model.summary()
    return train_model


# passing the data to the model with the below to style, both work
model = gen_model2(x_train_it, y_train_it)
model.fit(epochs=20, steps_per_epoch=steps_per_epoch)
score = model.evaluate(test_ds, steps=128)
print(score)
score = model.evaluate(x_test_it, y_test_it, steps=128)
print(score)

 

posted @ 2019-01-11 19:35  Lucas_Yu  阅读(3727)  评论(0编辑  收藏  举报